package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class SpringCacheSyncHandler {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IServeService serveService;

    //更新缓存---开通区域
    @XxlJob("activeRegionCacheSync")
    public void activeRegionCacheSync(){
        // 1.删除区域缓存
        redisTemplate.delete(RedisConstants.CacheName.JZ_CACHE + "::ACTIVE_REGIONS");

        // 2.重新执行查询方法
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();

        for (RegionSimpleResDTO regionSimpleResDTO : regionSimpleResDTOS) {
            //1.获取已开通区域的id
            Long id = regionSimpleResDTO.getId();

            //删除并查询更新区域服务缓存
            redisTemplate.delete(RedisConstants.CacheName.SERVE_ICON + "::" + id);
            serveService.firstPageServeList(id);

            //删除并查询更新热门服务缓存
            redisTemplate.delete(RedisConstants.CacheName.HOT_SERVE + "::" + id);
            serveService.hotServeList(id);
        }
    }
}
